<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>buffer</title>
<link rel="stylesheet" href="../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../boost_asio.html" title="Boost.Asio">
<link rel="up" href="../reference.html" title="Reference">
<link rel="prev" href="bind_executor/overload2.html" title="bind_executor (2 of 2 overloads)">
<link rel="next" href="buffer/overload1.html" title="buffer (1 of 36 overloads)">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../boost.png"></td>
<td align="center"><a href="../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="bind_executor/overload2.html"><img src="../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../reference.html"><img src="../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../boost_asio.html"><img src="../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="buffer/overload1.html"><img src="../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_asio.reference.buffer"></a><a class="link" href="buffer.html" title="buffer">buffer</a>
</h3></div></div></div>
<p>
        <a class="indexterm" name="boost_asio.indexterm.buffer"></a> 
The <code class="computeroutput">boost::asio::buffer</code> function
        is used to create a buffer object to represent raw memory, an array of POD
        elements, a vector of POD elements, or a std::string.
      </p>
<p>
        Create a new modifiable buffer from an existing buffer.
      </p>
<pre class="programlisting">mutable_buffer <a class="link" href="buffer/overload1.html" title="buffer (1 of 36 overloads)">buffer</a>(
    const mutable_buffer &amp; b);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload1.html" title="buffer (1 of 36 overloads)">more...</a></em></span>

mutable_buffer <a class="link" href="buffer/overload2.html" title="buffer (2 of 36 overloads)">buffer</a>(
    const mutable_buffer &amp; b,
    std::size_t max_size_in_bytes);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload2.html" title="buffer (2 of 36 overloads)">more...</a></em></span>
</pre>
<p>
        Create a new non-modifiable buffer from an existing buffer.
      </p>
<pre class="programlisting">const_buffer <a class="link" href="buffer/overload3.html" title="buffer (3 of 36 overloads)">buffer</a>(
    const const_buffer &amp; b);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload3.html" title="buffer (3 of 36 overloads)">more...</a></em></span>

const_buffer <a class="link" href="buffer/overload4.html" title="buffer (4 of 36 overloads)">buffer</a>(
    const const_buffer &amp; b,
    std::size_t max_size_in_bytes);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload4.html" title="buffer (4 of 36 overloads)">more...</a></em></span>
</pre>
<p>
        Create a new modifiable buffer that represents the given memory range.
      </p>
<pre class="programlisting">mutable_buffer <a class="link" href="buffer/overload5.html" title="buffer (5 of 36 overloads)">buffer</a>(
    void * data,
    std::size_t size_in_bytes);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload5.html" title="buffer (5 of 36 overloads)">more...</a></em></span>
</pre>
<p>
        Create a new non-modifiable buffer that represents the given memory range.
      </p>
<pre class="programlisting">const_buffer <a class="link" href="buffer/overload6.html" title="buffer (6 of 36 overloads)">buffer</a>(
    const void * data,
    std::size_t size_in_bytes);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload6.html" title="buffer (6 of 36 overloads)">more...</a></em></span>
</pre>
<p>
        Create a new modifiable buffer that represents the given POD array.
      </p>
<pre class="programlisting">template&lt;
    typename PodType,
    std::size_t N&gt;
mutable_buffer <a class="link" href="buffer/overload7.html" title="buffer (7 of 36 overloads)">buffer</a>(
    PodType (&amp;data)[N]);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload7.html" title="buffer (7 of 36 overloads)">more...</a></em></span>

template&lt;
    typename PodType,
    std::size_t N&gt;
mutable_buffer <a class="link" href="buffer/overload8.html" title="buffer (8 of 36 overloads)">buffer</a>(
    PodType (&amp;data)[N],
    std::size_t max_size_in_bytes);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload8.html" title="buffer (8 of 36 overloads)">more...</a></em></span>
</pre>
<p>
        Create a new non-modifiable buffer that represents the given POD array.
      </p>
<pre class="programlisting">template&lt;
    typename PodType,
    std::size_t N&gt;
const_buffer <a class="link" href="buffer/overload9.html" title="buffer (9 of 36 overloads)">buffer</a>(
    const PodType (&amp;data)[N]);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload9.html" title="buffer (9 of 36 overloads)">more...</a></em></span>

template&lt;
    typename PodType,
    std::size_t N&gt;
const_buffer <a class="link" href="buffer/overload10.html" title="buffer (10 of 36 overloads)">buffer</a>(
    const PodType (&amp;data)[N],
    std::size_t max_size_in_bytes);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload10.html" title="buffer (10 of 36 overloads)">more...</a></em></span>
</pre>
<p>
        Create a new modifiable buffer that represents the given POD array.
      </p>
<pre class="programlisting">template&lt;
    typename PodType,
    std::size_t N&gt;
mutable_buffer <a class="link" href="buffer/overload11.html" title="buffer (11 of 36 overloads)">buffer</a>(
    boost::array&lt; PodType, N &gt; &amp; data);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload11.html" title="buffer (11 of 36 overloads)">more...</a></em></span>

template&lt;
    typename PodType,
    std::size_t N&gt;
mutable_buffer <a class="link" href="buffer/overload12.html" title="buffer (12 of 36 overloads)">buffer</a>(
    boost::array&lt; PodType, N &gt; &amp; data,
    std::size_t max_size_in_bytes);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload12.html" title="buffer (12 of 36 overloads)">more...</a></em></span>
</pre>
<p>
        Create a new non-modifiable buffer that represents the given POD array.
      </p>
<pre class="programlisting">template&lt;
    typename PodType,
    std::size_t N&gt;
const_buffer <a class="link" href="buffer/overload13.html" title="buffer (13 of 36 overloads)">buffer</a>(
    boost::array&lt; const PodType, N &gt; &amp; data);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload13.html" title="buffer (13 of 36 overloads)">more...</a></em></span>

template&lt;
    typename PodType,
    std::size_t N&gt;
const_buffer <a class="link" href="buffer/overload14.html" title="buffer (14 of 36 overloads)">buffer</a>(
    boost::array&lt; const PodType, N &gt; &amp; data,
    std::size_t max_size_in_bytes);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload14.html" title="buffer (14 of 36 overloads)">more...</a></em></span>

template&lt;
    typename PodType,
    std::size_t N&gt;
const_buffer <a class="link" href="buffer/overload15.html" title="buffer (15 of 36 overloads)">buffer</a>(
    const boost::array&lt; PodType, N &gt; &amp; data);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload15.html" title="buffer (15 of 36 overloads)">more...</a></em></span>

template&lt;
    typename PodType,
    std::size_t N&gt;
const_buffer <a class="link" href="buffer/overload16.html" title="buffer (16 of 36 overloads)">buffer</a>(
    const boost::array&lt; PodType, N &gt; &amp; data,
    std::size_t max_size_in_bytes);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload16.html" title="buffer (16 of 36 overloads)">more...</a></em></span>
</pre>
<p>
        Create a new modifiable buffer that represents the given POD array.
      </p>
<pre class="programlisting">template&lt;
    typename PodType,
    std::size_t N&gt;
mutable_buffer <a class="link" href="buffer/overload17.html" title="buffer (17 of 36 overloads)">buffer</a>(
    std::array&lt; PodType, N &gt; &amp; data);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload17.html" title="buffer (17 of 36 overloads)">more...</a></em></span>

template&lt;
    typename PodType,
    std::size_t N&gt;
mutable_buffer <a class="link" href="buffer/overload18.html" title="buffer (18 of 36 overloads)">buffer</a>(
    std::array&lt; PodType, N &gt; &amp; data,
    std::size_t max_size_in_bytes);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload18.html" title="buffer (18 of 36 overloads)">more...</a></em></span>
</pre>
<p>
        Create a new non-modifiable buffer that represents the given POD array.
      </p>
<pre class="programlisting">template&lt;
    typename PodType,
    std::size_t N&gt;
const_buffer <a class="link" href="buffer/overload19.html" title="buffer (19 of 36 overloads)">buffer</a>(
    std::array&lt; const PodType, N &gt; &amp; data);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload19.html" title="buffer (19 of 36 overloads)">more...</a></em></span>

template&lt;
    typename PodType,
    std::size_t N&gt;
const_buffer <a class="link" href="buffer/overload20.html" title="buffer (20 of 36 overloads)">buffer</a>(
    std::array&lt; const PodType, N &gt; &amp; data,
    std::size_t max_size_in_bytes);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload20.html" title="buffer (20 of 36 overloads)">more...</a></em></span>

template&lt;
    typename PodType,
    std::size_t N&gt;
const_buffer <a class="link" href="buffer/overload21.html" title="buffer (21 of 36 overloads)">buffer</a>(
    const std::array&lt; PodType, N &gt; &amp; data);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload21.html" title="buffer (21 of 36 overloads)">more...</a></em></span>

template&lt;
    typename PodType,
    std::size_t N&gt;
const_buffer <a class="link" href="buffer/overload22.html" title="buffer (22 of 36 overloads)">buffer</a>(
    const std::array&lt; PodType, N &gt; &amp; data,
    std::size_t max_size_in_bytes);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload22.html" title="buffer (22 of 36 overloads)">more...</a></em></span>
</pre>
<p>
        Create a new modifiable buffer that represents the given POD vector.
      </p>
<pre class="programlisting">template&lt;
    typename PodType,
    typename Allocator&gt;
mutable_buffer <a class="link" href="buffer/overload23.html" title="buffer (23 of 36 overloads)">buffer</a>(
    std::vector&lt; PodType, Allocator &gt; &amp; data);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload23.html" title="buffer (23 of 36 overloads)">more...</a></em></span>

template&lt;
    typename PodType,
    typename Allocator&gt;
mutable_buffer <a class="link" href="buffer/overload24.html" title="buffer (24 of 36 overloads)">buffer</a>(
    std::vector&lt; PodType, Allocator &gt; &amp; data,
    std::size_t max_size_in_bytes);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload24.html" title="buffer (24 of 36 overloads)">more...</a></em></span>
</pre>
<p>
        Create a new non-modifiable buffer that represents the given POD vector.
      </p>
<pre class="programlisting">template&lt;
    typename PodType,
    typename Allocator&gt;
const_buffer <a class="link" href="buffer/overload25.html" title="buffer (25 of 36 overloads)">buffer</a>(
    const std::vector&lt; PodType, Allocator &gt; &amp; data);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload25.html" title="buffer (25 of 36 overloads)">more...</a></em></span>

template&lt;
    typename PodType,
    typename Allocator&gt;
const_buffer <a class="link" href="buffer/overload26.html" title="buffer (26 of 36 overloads)">buffer</a>(
    const std::vector&lt; PodType, Allocator &gt; &amp; data,
    std::size_t max_size_in_bytes);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload26.html" title="buffer (26 of 36 overloads)">more...</a></em></span>
</pre>
<p>
        Create a new modifiable buffer that represents the given string.
      </p>
<pre class="programlisting">template&lt;
    typename Elem,
    typename Traits,
    typename Allocator&gt;
mutable_buffer <a class="link" href="buffer/overload27.html" title="buffer (27 of 36 overloads)">buffer</a>(
    std::basic_string&lt; Elem, Traits, Allocator &gt; &amp; data);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload27.html" title="buffer (27 of 36 overloads)">more...</a></em></span>

template&lt;
    typename Elem,
    typename Traits,
    typename Allocator&gt;
mutable_buffer <a class="link" href="buffer/overload28.html" title="buffer (28 of 36 overloads)">buffer</a>(
    std::basic_string&lt; Elem, Traits, Allocator &gt; &amp; data,
    std::size_t max_size_in_bytes);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload28.html" title="buffer (28 of 36 overloads)">more...</a></em></span>
</pre>
<p>
        Create a new non-modifiable buffer that represents the given string.
      </p>
<pre class="programlisting">template&lt;
    typename Elem,
    typename Traits,
    typename Allocator&gt;
const_buffer <a class="link" href="buffer/overload29.html" title="buffer (29 of 36 overloads)">buffer</a>(
    const std::basic_string&lt; Elem, Traits, Allocator &gt; &amp; data);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload29.html" title="buffer (29 of 36 overloads)">more...</a></em></span>

template&lt;
    typename Elem,
    typename Traits,
    typename Allocator&gt;
const_buffer <a class="link" href="buffer/overload30.html" title="buffer (30 of 36 overloads)">buffer</a>(
    const std::basic_string&lt; Elem, Traits, Allocator &gt; &amp; data,
    std::size_t max_size_in_bytes);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload30.html" title="buffer (30 of 36 overloads)">more...</a></em></span>
</pre>
<p>
        Create a new modifiable buffer that represents the given string_view.
      </p>
<pre class="programlisting">template&lt;
    typename Elem,
    typename Traits&gt;
const_buffer <a class="link" href="buffer/overload31.html" title="buffer (31 of 36 overloads)">buffer</a>(
    basic_string_view&lt; Elem, Traits &gt; data);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload31.html" title="buffer (31 of 36 overloads)">more...</a></em></span>
</pre>
<p>
        Create a new non-modifiable buffer that represents the given string.
      </p>
<pre class="programlisting">template&lt;
    typename Elem,
    typename Traits&gt;
const_buffer <a class="link" href="buffer/overload32.html" title="buffer (32 of 36 overloads)">buffer</a>(
    basic_string_view&lt; Elem, Traits &gt; data,
    std::size_t max_size_in_bytes);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload32.html" title="buffer (32 of 36 overloads)">more...</a></em></span>
</pre>
<p>
        Obtain a buffer representing the entire registered buffer.
      </p>
<pre class="programlisting">mutable_registered_buffer <a class="link" href="buffer/overload33.html" title="buffer (33 of 36 overloads)">buffer</a>(
    const mutable_registered_buffer &amp; b);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload33.html" title="buffer (33 of 36 overloads)">more...</a></em></span>

const_registered_buffer <a class="link" href="buffer/overload34.html" title="buffer (34 of 36 overloads)">buffer</a>(
    const const_registered_buffer &amp; b);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload34.html" title="buffer (34 of 36 overloads)">more...</a></em></span>
</pre>
<p>
        Obtain a buffer representing part of a registered buffer.
      </p>
<pre class="programlisting">mutable_registered_buffer <a class="link" href="buffer/overload35.html" title="buffer (35 of 36 overloads)">buffer</a>(
    const mutable_registered_buffer &amp; b,
    std::size_t n);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload35.html" title="buffer (35 of 36 overloads)">more...</a></em></span>

const_registered_buffer <a class="link" href="buffer/overload36.html" title="buffer (36 of 36 overloads)">buffer</a>(
    const const_registered_buffer &amp; b,
    std::size_t n);
  <span class="emphasis"><em>» <a class="link" href="buffer/overload36.html" title="buffer (36 of 36 overloads)">more...</a></em></span>
</pre>
<p>
        A buffer object represents a contiguous region of memory as a 2-tuple consisting
        of a pointer and size in bytes. A tuple of the form <code class="computeroutput">{void*, size_t}</code>
        specifies a mutable (modifiable) region of memory. Similarly, a tuple of
        the form <code class="computeroutput">{const void*, size_t}</code> specifies a const (non-modifiable)
        region of memory. These two forms correspond to the classes <a class="link" href="mutable_buffer.html" title="mutable_buffer"><code class="computeroutput">mutable_buffer</code></a>
        and <a class="link" href="const_buffer.html" title="const_buffer"><code class="computeroutput">const_buffer</code></a>,
        respectively. To mirror C++'s conversion rules, a <a class="link" href="mutable_buffer.html" title="mutable_buffer"><code class="computeroutput">mutable_buffer</code></a>
        is implicitly convertible to a <a class="link" href="const_buffer.html" title="const_buffer"><code class="computeroutput">const_buffer</code></a>,
        and the opposite conversion is not permitted.
      </p>
<p>
        The simplest use case involves reading or writing a single buffer of a specified
        size:
      </p>
<pre class="programlisting">sock.send(boost::asio::buffer(data, size));
</pre>
<p>
        In the above example, the return value of <code class="computeroutput">boost::asio::buffer</code>
        meets the requirements of the ConstBufferSequence concept so that it may
        be directly passed to the socket's write function. A buffer created for modifiable
        memory also meets the requirements of the MutableBufferSequence concept.
      </p>
<p>
        An individual buffer may be created from a builtin array, std::vector, std::array
        or boost::array of POD elements. This helps prevent buffer overruns by automatically
        determining the size of the buffer:
      </p>
<pre class="programlisting">char d1[128];
size_t bytes_transferred = sock.receive(boost::asio::buffer(d1));

std::vector&lt;char&gt; d2(128);
bytes_transferred = sock.receive(boost::asio::buffer(d2));

std::array&lt;char, 128&gt; d3;
bytes_transferred = sock.receive(boost::asio::buffer(d3));

boost::array&lt;char, 128&gt; d4;
bytes_transferred = sock.receive(boost::asio::buffer(d4));
</pre>
<p>
        In all three cases above, the buffers created are exactly 128 bytes long.
        Note that a vector is <span class="emphasis"><em>never</em></span> automatically resized when
        creating or using a buffer. The buffer size is determined using the vector's
        <code class="computeroutput">size()</code> member function, and not its capacity.
      </p>
<h5>
<a name="boost_asio.reference.buffer.h0"></a>
        <span class="phrase"><a name="boost_asio.reference.buffer.accessing_buffer_contents"></a></span><a class="link" href="buffer.html#boost_asio.reference.buffer.accessing_buffer_contents">Accessing
        Buffer Contents</a>
      </h5>
<p>
        The contents of a buffer may be accessed using the <code class="computeroutput">data()</code> and
        <code class="computeroutput">size()</code> member functions:
      </p>
<pre class="programlisting">boost::asio::mutable_buffer b1 = ...;
std::size_t s1 = b1.size();
unsigned char* p1 = static_cast&lt;unsigned char*&gt;(b1.data());

boost::asio::const_buffer b2 = ...;
std::size_t s2 = b2.size();
const void* p2 = b2.data();
</pre>
<p>
        The <code class="computeroutput">data()</code> member function permits violations of type safety,
        so uses of it in application code should be carefully considered.
      </p>
<p>
        For convenience, a <a class="link" href="buffer_size.html" title="buffer_size"><code class="computeroutput">buffer_size</code></a>
        function is provided that works with both buffers and buffer sequences (that
        is, types meeting the ConstBufferSequence or MutableBufferSequence type requirements).
        In this case, the function returns the total size of all buffers in the sequence.
      </p>
<h5>
<a name="boost_asio.reference.buffer.h1"></a>
        <span class="phrase"><a name="boost_asio.reference.buffer.buffer_copying"></a></span><a class="link" href="buffer.html#boost_asio.reference.buffer.buffer_copying">Buffer
        Copying</a>
      </h5>
<p>
        The <a class="link" href="buffer_copy.html" title="buffer_copy"><code class="computeroutput">buffer_copy</code></a>
        function may be used to copy raw bytes between individual buffers and buffer
        sequences.
      </p>
<p>
        In particular, when used with the <a class="link" href="buffer_size.html" title="buffer_size"><code class="computeroutput">buffer_size</code></a>
        function, the <a class="link" href="buffer_copy.html" title="buffer_copy"><code class="computeroutput">buffer_copy</code></a>
        function can be used to linearise a sequence of buffers. For example:
      </p>
<pre class="programlisting">vector&lt;const_buffer&gt; buffers = ...;

vector&lt;unsigned char&gt; data(boost::asio::buffer_size(buffers));
boost::asio::buffer_copy(boost::asio::buffer(data), buffers);
</pre>
<p>
        Note that <a class="link" href="buffer_copy.html" title="buffer_copy"><code class="computeroutput">buffer_copy</code></a>
        is implemented in terms of <code class="computeroutput">memcpy</code>, and consequently it cannot
        be used to copy between overlapping memory regions.
      </p>
<h5>
<a name="boost_asio.reference.buffer.h2"></a>
        <span class="phrase"><a name="boost_asio.reference.buffer.buffer_invalidation"></a></span><a class="link" href="buffer.html#boost_asio.reference.buffer.buffer_invalidation">Buffer
        Invalidation</a>
      </h5>
<p>
        A buffer object does not have any ownership of the memory it refers to. It
        is the responsibility of the application to ensure the memory region remains
        valid until it is no longer required for an I/O operation. When the memory
        is no longer available, the buffer is said to have been invalidated.
      </p>
<p>
        For the <code class="computeroutput">boost::asio::buffer</code> overloads that accept an argument
        of type std::vector, the buffer objects returned are invalidated by any vector
        operation that also invalidates all references, pointers and iterators referring
        to the elements in the sequence (C++ Std, 23.2.4)
      </p>
<p>
        For the <code class="computeroutput">boost::asio::buffer</code> overloads that accept an argument
        of type std::basic_string, the buffer objects returned are invalidated according
        to the rules defined for invalidation of references, pointers and iterators
        referring to elements of the sequence (C++ Std, 21.3).
      </p>
<h5>
<a name="boost_asio.reference.buffer.h3"></a>
        <span class="phrase"><a name="boost_asio.reference.buffer.buffer_arithmetic"></a></span><a class="link" href="buffer.html#boost_asio.reference.buffer.buffer_arithmetic">Buffer
        Arithmetic</a>
      </h5>
<p>
        Buffer objects may be manipulated using simple arithmetic in a safe way which
        helps prevent buffer overruns. Consider an array initialised as follows:
      </p>
<pre class="programlisting">boost::array&lt;char, 6&gt; a = { 'a', 'b', 'c', 'd', 'e' };
</pre>
<p>
        A buffer object <code class="computeroutput">b1</code> created using:
      </p>
<pre class="programlisting">b1 = boost::asio::buffer(a);
</pre>
<p>
        represents the entire array, <code class="computeroutput">{ 'a', 'b', 'c', 'd', 'e' }</code>. An
        optional second argument to the <code class="computeroutput">boost::asio::buffer</code> function
        may be used to limit the size, in bytes, of the buffer:
      </p>
<pre class="programlisting">b2 = boost::asio::buffer(a, 3);
</pre>
<p>
        such that <code class="computeroutput">b2</code> represents the data <code class="computeroutput">{ 'a', 'b', 'c' }</code>.
        Even if the size argument exceeds the actual size of the array, the size
        of the buffer object created will be limited to the array size.
      </p>
<p>
        An offset may be applied to an existing buffer to create a new one:
      </p>
<pre class="programlisting">b3 = b1 + 2;
</pre>
<p>
        where <code class="computeroutput">b3</code> will set to represent <code class="computeroutput">{ 'c', 'd', 'e' }</code>.
        If the offset exceeds the size of the existing buffer, the newly created
        buffer will be empty.
      </p>
<p>
        Both an offset and size may be specified to create a buffer that corresponds
        to a specific range of bytes within an existing buffer:
      </p>
<pre class="programlisting">b4 = boost::asio::buffer(b1 + 1, 3);
</pre>
<p>
        so that <code class="computeroutput">b4</code> will refer to the bytes <code class="computeroutput">{ 'b', 'c', 'd' }</code>.
      </p>
<h5>
<a name="boost_asio.reference.buffer.h4"></a>
        <span class="phrase"><a name="boost_asio.reference.buffer.buffers_and_scatter_gather_i_o"></a></span><a class="link" href="buffer.html#boost_asio.reference.buffer.buffers_and_scatter_gather_i_o">Buffers
        and Scatter-Gather I/O</a>
      </h5>
<p>
        To read or write using multiple buffers (i.e. scatter-gather I/O), multiple
        buffer objects may be assigned into a container that supports the MutableBufferSequence
        (for read) or ConstBufferSequence (for write) concepts:
      </p>
<pre class="programlisting">char d1[128];
std::vector&lt;char&gt; d2(128);
boost::array&lt;char, 128&gt; d3;

boost::array&lt;mutable_buffer, 3&gt; bufs1 = {
  boost::asio::buffer(d1),
  boost::asio::buffer(d2),
  boost::asio::buffer(d3) };
bytes_transferred = sock.receive(bufs1);

std::vector&lt;const_buffer&gt; bufs2;
bufs2.push_back(boost::asio::buffer(d1));
bufs2.push_back(boost::asio::buffer(d2));
bufs2.push_back(boost::asio::buffer(d3));
bytes_transferred = sock.send(bufs2);
</pre>
<h5>
<a name="boost_asio.reference.buffer.h5"></a>
        <span class="phrase"><a name="boost_asio.reference.buffer.requirements"></a></span><a class="link" href="buffer.html#boost_asio.reference.buffer.requirements">Requirements</a>
      </h5>
<p>
        <span class="emphasis"><em>Header: </em></span><code class="literal">boost/asio/buffer.hpp</code>
      </p>
<p>
        <span class="emphasis"><em>Convenience header: </em></span><code class="literal">boost/asio.hpp</code>
      </p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2003-2021 Christopher
      M. Kohlhoff<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="bind_executor/overload2.html"><img src="../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../reference.html"><img src="../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../boost_asio.html"><img src="../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="buffer/overload1.html"><img src="../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
